package com.link.base.base.autoconfig.autoconfig.controller;

import com.link.base.base.autoconfig.autoconfig.model.LnkInnerObject;
import com.link.base.base.autoconfig.autoconfig.service.LnkInnerObjectService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.modules.BatchExportFileWriter;
import com.link.core.modules.export.ExportTextFileWriter;
import com.link.core.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 自动配置——对象信息
 *
 * @author 姚彭健
 * @date 2019-12-16 10:50
 */
@Controller("LnkInnerObjectController")
@RequestMapping("/link/autoconfig")
public class LnkInnerObjectController extends BasicController<LnkInnerObject> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource(name = "lnkInnerObjectService")
    private LnkInnerObjectService service;

    @Resource(name = "lnkInnerObjectMySqlService")
    private LnkInnerObjectService mysqlService;

    private static final Logger logger = LogManager.getLogger(LnkInnerObjectController.class);

    @Override
    public BasicService<LnkInnerObject> getBasicService() throws Exception {
        return service;
    }

    /**
     * 重写父类beforeUpsert方法校验表名长度
     *
     * @author 姚彭健
     * @date 2019-07-17
     * @param entity 要校验的表对象
     * @param request request
     * @throws Exception 校验异常
     *
     */
    @Override
    public void beforUpsert(LnkInnerObject entity, HttpServletRequest request) throws Exception {
        // 校验表名长度是否超过20
        boolean checkPassed = StringUtils.checkTable(entity.getBasetable(), 20);
        if (!checkPassed) {
            throw new ServiceException("AUTOCONFIG-001");
        }
    }

    /**
     * 生成建表语句
     *
     * @author 姚彭健
     * @date 2019-12-16 16:37
     * @param record 要新建的表对象
     * @param session session
     * @param request request
     * @param response response
     */
    @RequestMapping(value = "/newTable", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> newTable(@JsonParam LnkInnerObject record, HttpSession session, HttpServletRequest request,
                                        HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        ExportTextFileWriter writer = null;
        try {
            writer = new ExportTextFileWriter();
            String text = "";
            if ("MySql".equals(record.getAttr1())) {
                text = mysqlService.newTable(record);
            } else {
                text = service.newTable(record);
            }
            writer.writeText2(text);
            System.out.print(writer.getFilePath());
            System.out.print(writer.getUrlPath());
            result.put("url", writer.getUrlPath());
            result.put("success", true);
        } catch (Exception e) {
            logger.error("LnkInnerObjectController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 生成sqlMapper代码
     *
     * @author 姚彭健
     * @date 2019-12-16 16:38
     * @param record 表对象
     * @param session session
     * @param request request
     * @param response response
     */
    @RequestMapping(value = "/newSqlMapper", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> Test(@JsonParam LnkInnerObject record, HttpSession session, HttpServletRequest request,
                                    HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        ExportTextFileWriter writer = null;
        try {
            writer = new ExportTextFileWriter();
            String text = "";
            String database = record.getDatabaseType();
            record = service.getFullLnkInnerObjectById(record);
            text = service.newSqlMapper(record, database, true);
            writer.writeText2(text);
            System.out.print(writer.getFilePath());
            System.out.print(writer.getUrlPath());
            result.put("url", writer.getUrlPath());
            result.put("success", true);
        } catch (Exception e) {
            logger.error("LnkInnerObjectController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 生成查询sql
     *
     * @author 姚彭健
     * @date 2019-12-16 16:39
     * @param record 表对象
     * @param session session
     * @param request request
     * @param response response
     */
    @RequestMapping(value = "/newQueryTable", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> newQueryTable(@JsonParam LnkInnerObject record, HttpSession session, HttpServletRequest request,
                                             HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        ExportTextFileWriter writer = null;
        try {
            writer = new ExportTextFileWriter();

            String text = "";
            if ("MySql".equals(record.getAttr1())) {
                text = mysqlService.newQueryTable(record);
            } else {
                text = service.newQueryTable(record);
            }
            writer.writeText2(text);
            System.out.print(writer.getFilePath());
            System.out.print(writer.getUrlPath());
            result.put("url", writer.getUrlPath());
            result.put("success", true);
        } catch (Exception e) {
            logger.error("LnkInnerObjectController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 生成对象文件
     *
     * @author 姚彭健
     * @date 2019-12-16 16:39
     * @param record 表对象
     * @param session session
     * @param request request
     * @param response response
     */
    @RequestMapping(value = "/newObjectFile", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> newObjectFile(@JsonParam LnkInnerObject record, HttpSession session, HttpServletRequest request,
                                             HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        BatchExportFileWriter writer = null;
        try {
            writer = new BatchExportFileWriter();
            writer.writeBatchText(service.newObjectFile(record));

            writer.getFilePath();
            writer.getUrlPath();
            result.put("url", writer.getUrlZipPath());
            result.put("success", true);
        } catch (Exception e) {
            logger.error("LnkInnerObjectController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /*@RequestMapping(value = "/newAppFile", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> newAppFile(LnkInnerObject record, HttpSession session, HttpServletRequest request,
                                          HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        BatchExportFileWriter writer = null;
        try {
            writer = new BatchExportFileWriter();
            writer.writeBatchText(service.newAppFile(record));
            writer.getFilePath();
            writer.getUrlPath();
            result.put("url", writer.getUrlZipPath());
            result.put("success", true);
        } catch (Exception e) {
            logger.error("LnkInnerObjectController捕获异常: ",e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }*/

    @RequestMapping(value = "/getmodelinfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getmodelinfo(@JsonParam LnkInnerObject record, HttpSession session, HttpServletRequest request,
                                            HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            service.getmodelinfo(record);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("LnkInnerObjectController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/updatemodelinfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updatemodelinfo(@JsonParam LnkInnerObject record, HttpSession session, HttpServletRequest request,
                                               HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            service.updatemodelinfo(record);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("LnkInnerObjectController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/Check", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> Check(@JsonParam LnkInnerObject record, HttpSession session, HttpServletRequest request,
                                     HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            service.Check(record);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("LnkInnerObjectController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/deepCopySub", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> DeepCopy(@JsonParam LnkInnerObject record, HttpSession session, HttpServletRequest request,
                                        HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            service.DeepCopy(record);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("LnkInnerObjectController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
